//
//  VideoScrollView.swift
//  SwiftMusicPlayer
//
//  Created by 顾佳槟 on 2018/3/7.
//  Copyright © 2018年 lancoo. All rights reserved.
//

import UIKit

typealias ScrollViewDidScrollBlock = (CGFloat) -> Void
typealias ScrollViewEndScrollBlock = (Int) -> Void

class VideoScrollView: UIScrollView,UIScrollViewDelegate {

    //个人中心界面
    var peopleCenterView:PeopleCenterView?
    //推荐界面
    var recommendView:RecommendView?
    //乐馆
    var musicHouseView:MusicHouseView?
    //正在滚动Block
    var scrollViewDidScrollBlock:ScrollViewDidScrollBlock?
    //滚动结束Block
    var scrollViewEndScrollBlock:ScrollViewEndScrollBlock?
    //是否由外部控制滚动
    var isScrollByOutside:Bool = false
    //水平滚动距离
    var contentOffsetX:CGFloat = 0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupSubviews()
        config()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupSubviews(){
        self.peopleCenterView = PeopleCenterView.init(frame:CGRect.zero)
        self.addSubview(peopleCenterView!)
        self.peopleCenterView!.mas_makeConstraints({ (make) in
            make?.left.setOffset(0)
            make?.top.equalTo()(self)
            make?.height.equalTo()(self)
            make?.width.setOffset(kScreenWidth)
        })
        
        self.recommendView = RecommendView.init(frame:CGRect.zero)
        self.addSubview(recommendView!)
        self.recommendView!.mas_makeConstraints({ (make) in
            make?.left.setOffset(kScreenWidth)
            make?.top.equalTo()(self)
            make?.height.equalTo()(self)
            make?.width.setOffset(kScreenWidth)
        })
        
        self.musicHouseView = MusicHouseView.init(frame:CGRect.zero)
        self.addSubview(musicHouseView!)
        self.musicHouseView!.mas_makeConstraints({ (make) in
            make?.left.setOffset(kScreenWidth * 2)
            make?.top.equalTo()(self)
            make?.height.equalTo()(self)
            make?.width.setOffset(kScreenWidth)
        })
    }
    
    func config(){
        self.contentSize = CGSize.init(width: kScreenWidth * 3, height: (kScreenHeight-kNavigationHeight - kVideoPlayerHeight))
        self.delegate = self
        self.backgroundColor = UIColor.init(hexString: "F5F5F5")
        self.showsHorizontalScrollIndicator = false
        self.isPagingEnabled = true
        self.bounces = false
        self.showsVerticalScrollIndicator = false
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        
    }
    
    //UIscrollView的协议代理方法
    //滑动过程中
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        
        let offsetX = offset.x - contentOffsetX
        
        contentOffsetX = offset.x
        
        if self.scrollViewDidScrollBlock != nil && !self.isScrollByOutside{
            self.scrollViewDidScrollBlock!(offsetX)
        }
    }
    //开始拖动
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        
    }
    //开始拖动（以某种速率和偏移量）
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        //更新UIPageControl的当前页
        let offset = scrollView.contentOffset;
        let bounds = scrollView.frame;
        
        let index = offset.x / bounds.size.width
        
        self.isScrollByOutside = true
        
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
            self.contentOffset = CGPoint.init(x:index * kScreenWidth, y: 0)
        }, completion:{(flag) in
            self.isScrollByOutside = false
            self.contentOffsetX = index * kScreenWidth
        })
                
        if self.scrollViewEndScrollBlock != nil {
            self.scrollViewEndScrollBlock!(Int(index))
        }
    }
    
    //停止拖动
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        
    }
    //开始滑动
    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
        
    }
}

extension VideoScrollView{
    func scrollToView(index:Int){
        self.isScrollByOutside = true
        
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
            self.contentOffset = CGPoint.init(x:CGFloat(index-1)*kScreenWidth, y: 0)
        }, completion:{(flag) in
            self.isScrollByOutside = false
        })
    }
}
